# Forked from Alpine to rebuild with systemd support

maintainer="Jane Rachinger <jane400+pmaports@postmarketos.org>"
pkgname=polkit
pkgver=999125
_pkgver=125
pkgrel=2
pkgdesc="Application development toolkit for controlling system-wide privileges"
url="https://github.com/polkit-org/polkit"
arch="all"
license="GPL-2.0-or-later"
options="suid !check"  # Needs a functioning dbus daemon
makedepends="
	dbus-dev
	duktape-dev
	systemd-dev
	expat-dev
	glib-dev
	gobject-introspection-dev
	linux-pam-dev
	meson
	perl
	"
pkgusers="polkitd"
pkggroups="polkitd"
install="$pkgname.pre-install $pkgname.pre-upgrade"
subpackages="
	$pkgname-dev
	$pkgname-libs
	$pkgname-doc
	$pkgname-lang
	$pkgname-systemd
	"
source="https://github.com/polkit-org/polkit/archive/refs/tags/$_pkgver/polkit-$_pkgver.tar.gz
	alpine-polkit.pam
	args.patch
	"
builddir="$srcdir/polkit-$_pkgver"
# Not running 'systemctl preset' on polkit
# Reasoning from fedora:
#  # The implied (systemctl preset) will fail and complain, but the macro hides
#  # and ignores the fact.  This is in fact what we want, polkit.service does not
#  # have an [Install] section and it is always started on demand.
# https://src.fedoraproject.org/rpms/polkit/blob/rawhide/f/polkit.spec

# secfixes:
#   0.120-r2:
#     - CVE-2021-4034
#   0.119-r0:
#     - CVE-2021-3560

build() {
	msg 'Building with systemd'
	_build systemd -Dsession_tracking="logind"
}

_build() {
	local outdir="$1"
	shift
	abuild-meson \
		-Db_lto=true \
		-Dtests="$(want_check && echo true || echo false)" \
		-Dman=true \
		-Dpam_prefix=/etc/pam.d \
		"$@" \
		. "$outdir"
	meson compile -C "$outdir"
}

check() {
	meson test --print-errorlogs --no-rebuild -C systemd
}

package() {
	depends="$pkgname-libs=$pkgver-r$pkgrel"
	provides="$pkgname-elogind=$pkgver-r$pkgrel"

	DESTDIR="$pkgdir" meson install --no-rebuild -C systemd

	cd "$pkgdir"

	# Use our own polkit rules, upstream may change them
	install -m644 "$srcdir"/alpine-polkit.pam etc/pam.d/polkit-1

	# See polkit's configure script which tells us what permissions to set
	chown -R polkitd:polkitd etc/polkit-1/rules.d usr/share/polkit-1/rules.d
	chmod -R 700 etc/polkit-1/rules.d usr/share/polkit-1/rules.d
	chmod 4755 usr/lib/polkit-1/polkit-agent-helper-1
	chmod 4755 usr/bin/pkexec
}

libs() {
	provides="polkit-elogind-libs polkit-noelogind-libs"
	provider_priority=9999

	default_libs
}

dev() {
	provides="polkit-elogind-dev polkit-noelogind-dev"
	provider_priority=9999

	default_dev
}

sha512sums="
64d85c1557355d6de6483beeb855b74a99dbb30cf9968206dc0aaf147156072ca2604bf667533099ee3972b3eed0421ec0a1ff8bea35a1e4c54da7b9688e0953  polkit-125.tar.gz
f5102dc00d390b3a3c957b62e1712db778c7ffb7378f3d8e816c0757c11a308c5d5303e42595b0d6add9839247c773880cd34e56afacc89eb6efaadf9aae7826  alpine-polkit.pam
0605c528e25ea1f281ea7708e0185df971174e809ef112c24caa18658b78c6b819890897a4e5d5353098c52be2fccea6a902049c846b3b6b713cb7501f431703  args.patch
"
